﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data.SqlClient;
using System.Data;
namespace Guline.wsjx.SqlExcute
{
    public class Provider
    {

        private SqlConnection connection;
        public Provider(string connectstring)
        {
            this.connection = new SqlConnection(connectstring);
        }
        private void Start()
        {
            if (this.connection.State != ConnectionState.Open)
            {
                this.connection.Open();
            }
        }
        private void End()
        {
            if (this.connection.State == ConnectionState.Open)
            {
                this.connection.Close();
            }
        }
        public DataTable excuteTable(string query)
        {   this.Start();
            DataTable t = new DataTable();
            try {
                SqlCommand cmd = new SqlCommand(query, this.connection);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(t);
            }
             catch{
               t=null;
             }
            this.End();
            return t;
        }
        public int excuteUpdate(string query)
        {
            try
            {

                this.Start();
                SqlCommand cmd = new SqlCommand(query, this.connection);
                int result = cmd.ExecuteNonQuery();
                this.End();
                return result;
            }
            catch {
                return 0;
            }
             
        }
    }
}